package com.base.system.managedBean.baseBean;

import java.io.Serializable;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.SelectItems;

import com.base.common.provider.SecuritySelectItemProvider;
import com.base.common.provider.SelectItemsProvider;
import com.base.organization.entity.CentralUnit;

public abstract class BaseCentralUnitBean implements Serializable {
	@Inject
	public FacesContext context;
	@Accessible
	@Bind
	protected String centralUnitId;
	@Bind(id = "unitCode", attribute = "value")
	protected String unitCode;
	@Bind(id = "type", attribute = "value")
	protected String type;
	@Bind(id = "name", attribute = "value")
	protected String name;
	@Bind(id = "indexNumber", attribute = "value")
	protected Integer indexNumber;
	@Accessible
	protected SelectItem[] enable_items = SelectItemsProvider.getYesNo();
	@Bind(id = "enable", attribute = "value")
	@SelectItems(source = "#{this.enable_items}")
	protected boolean enable = true;
	@Accessible
	private SelectItem[] parentCentralUnitId_items = SecuritySelectItemProvider
			.getAllCentralUnit(false);
	@Bind(id = "parentCentralUnitId")
	@SelectItems(source = "#{this.parentCentralUnitId_items}")
	protected String parentCentralUnitId;
	protected CentralUnit centralUnit;

	protected abstract void initSpecialProperty();

	protected void setCentralUnitProperty() {
		if (centralUnit == null)
			return;
		if (centralUnitId != null && !"".equals(centralUnitId))
			centralUnit.setCentralUnitId(Integer.parseInt(centralUnitId));
		centralUnit.setEnable(enable);
		centralUnit.setName(name);
		centralUnit.setParentCentralUnitId(parentCentralUnitId == null
				|| "".equals(parentCentralUnitId) ? null : Integer
				.parseInt(parentCentralUnitId));
		centralUnit.setType(type);
		centralUnit.setUnitCode(unitCode);
		centralUnit.setIndexNumber(indexNumber);
	}

	protected void getCentralUnitProperty() {
		if (centralUnit == null)
			return;
		centralUnitId = centralUnit.getCentralUnitId().toString();
		enable = centralUnit.isEnable();
		name = centralUnit.getName();
		parentCentralUnitId = centralUnit.getParentCentralUnitId() == null ? null
				: centralUnit.getParentCentralUnitId().toString();
		type = centralUnit.getType();
		unitCode = centralUnit.getUnitCode();
		indexNumber = centralUnit.getIndexNumber();
	}
}
